package com.zrj.controller;

import com.alibaba.fastjson.JSON;
import com.zrj.annotation.IgnoreToken;
import com.zrj.common.cache.TokenCacheManager;
import com.zrj.pojo.Result;
import com.zrj.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zhaorujie
 * @version 1.0
 * @date 2021/6/24 14:44
 */
@RestController
@RequestMapping("/api")
public class ContentController {

    @Autowired
    private ContentService contentService;


    @GetMapping("parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) throws Exception {
        return contentService.parseData(keyword);
    }

    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String, Object>> searchData(@PathVariable String keyword,
                                                @PathVariable int pageNo,
                                                @PathVariable int pageSize) throws IOException {
        return contentService.searchDataHighlight(keyword, pageNo, pageSize);
    }

    @IgnoreToken
    @GetMapping("/login")
    public String login(String username) {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        TokenCacheManager.cacheTokenDate(uuid, username);
        Map<String, Object> cacheData = TokenCacheManager.getCacheData(uuid);
        //System.out.println(cacheData);
        return JSON.toJSONString(cacheData);
    }
}
